/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

define(['adminapp'], function ($app) {
    if (g.register == 'true') {
        $app.navactive('wx_welcome');
    } else {
        if (g.menu == 'true') {
            $app.navactive('wx_menu');
        } else {
            $app.navactive('wx_reply');
        }
    }


    var alerttpl = Handlebars.compile($(g.alerttpl).html())
    var newstpl = Handlebars.compile($(g.newstpl).html())

    $('input[name="message_type"]').change(function() {
        var val = $(this).val();
        if (val == 'text') {
            $("#type-2").hide();
            $("#type-1").show();
        } else {
            $("#type-1").hide();
            $("#type-2").show();
        }
    });

   /* $app.formvalidator({
        'content' : {
            required : true
        }
    }, 'typeTextForm');*/

    $("#typeNewsForm").validate({
        errorElement  : 'span', //default input error message container
        errorClass    : 'help-block',
        highlight: function (element) { // hightlight error inputs
            $(element).closest('.form-group').addClass('has-error'); // set error class to the control group
        },
        unhighlight: function (element) { // revert the change done by hightlight
            $(element).closest('.form-group').removeClass('has-error'); // set error class to the control group
        },
        success: function (label) {
            label.closest('.form-group').removeClass('has-error'); // set success class to the control group
        }
    });
    $("#typeTextForm").validate({
        errorElement  : 'span', //default input error message container
        errorClass    : 'help-block',
        highlight: function (element) { // hightlight error inputs
            $(element).closest('.form-group').addClass('has-error'); // set error class to the control group
        },
        unhighlight: function (element) { // revert the change done by hightlight
            $(element).closest('.form-group').removeClass('has-error'); // set error class to the control group
        },
        success: function (label) {
            label.closest('.form-group').removeClass('has-error'); // set success class to the control group
        }
    });

    $("#addItem").click(function() {
        var items = $('div[name="item"]');
        if (items.length < 10) {
            var _t = new Date().getTime();
            var id = "id" + _t;
            var title = "title" + _t;
            var url = "url" + _t;
            var file = "file" + _t;
            var content = "content" + _t;

            var context = {
                id : id,
                title : title,
                url : url,
                file : file,
                content : content
            }
            $("#typeTwoEls").append(newstpl(context));
        } else {
            var context = {classname : 'alert-warning', message : '最少有10条图文消息'};
            $("#tip").html(alerttpl(context));
        }

    });

    $('#typeTwoEls').delegate('a[name="remove"]', 'click', function() {
        var items = $('div[name="item"]');
        if (items.length > 1) {
            var id = $(this).attr("panel");
            $("#" + id).remove();
        } else {
            var context = {classname : 'alert-warning', message : '最少有一条图文消息'};
            $("#tip").html(alerttpl(context));
        }
    });
});